package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Rect;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by david on 2014-06-05.
 */
public class QTYInputBox extends LinearLayout implements View.OnClickListener,View.OnFocusChangeListener{
    private EditText etMUnit,etSUnit;
    private TextView tvMUnit, tvSUnit;
    private Button btnOK;
    private InputMethodManager inputMethodManager;
    private int mUnit=0,sUnit=0;
    private OnValueInputListener onValueInputListener=null;

    public int getMUnit() {
        return mUnit;
    }

    public void setMUnit(int mUnit) {
        this.mUnit = mUnit;
        this.etMUnit.setText(mUnit>=0?String.valueOf(mUnit):"");
    }

    public int getSUnit() {
        return sUnit;
    }

    public void setSUnit(int sUnit) {
        this.sUnit = sUnit;
        this.etSUnit.setText(sUnit>=0?String.valueOf(sUnit):"");
    }

    public void setOnValueInputListener(OnValueInputListener onValueInputListener) {
        this.onValueInputListener = onValueInputListener;
    }

    public EditText getEtMUnit() {
        return etMUnit;
    }

    public EditText getEtSUnit() {
        return etSUnit;
    }

    public TextView getTvMUnit() {
        return tvMUnit;
    }

    public TextView getTvSUnit() {
        return tvSUnit;
    }

    public QTYInputBox(Context context) {
        super(context);
        inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        this.setOrientation(HORIZONTAL);
        this.setLayoutParams(new ViewGroup.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT));
        etMUnit = createEditText();
        tvMUnit = createTextView("");
        etSUnit = createEditText();
        tvSUnit = createTextView("");
        btnOK = new Button(getContext());
        btnOK.setText("確定");
        this.addView(btnOK, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btnOK.setOnClickListener(this);
    }

    private EditText createEditText(){
        EditText editText = new EditText(getContext());
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        this.addView(editText, layoutParams);
        return editText;
    }

    private TextView createTextView(String text){
        TextView textView = new TextView(getContext());
        textView.setText(text);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 3;
        layoutParams.rightMargin = 3;
        this.addView(textView, layoutParams);
        return textView;
    }

    private int getEditValue(EditText editText){
        String s = editText.getText().toString();
        return "".equals(s)?0:Integer.parseInt(s);
    }

    @Override
    public void onClick(View v) {
        inputMethodManager.hideSoftInputFromWindow(this.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        mUnit = getEditValue(etMUnit);
        sUnit = getEditValue(etSUnit);
        if (onValueInputListener!=null) {
            onValueInputListener.onValueInput(mUnit, sUnit);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            etMUnit.requestFocus();
            inputMethodManager.showSoftInput(etMUnit, InputMethodManager.SHOW_FORCED);
        } else {

        }
    }

    public interface OnValueInputListener{
        public void onValueInput(int m, int s);
    }
}
